package com.orangelife.mobile.util;

import java.io.File;

import android.content.Context;

/**
 * 清除缓存的类
 * 
 * @author yusicheng
 * 
 */
public class CleanCacheUtils {

	private static CleanCacheUtils Instance;
	private Context context;

	public CleanCacheUtils() {

	}

	public void init(Context context) {
		this.context = context;
	}

	public static CleanCacheUtils getInstance() {
		if (Instance == null) {
			Instance = new CleanCacheUtils();
		}
		return Instance;
	}

	/**
	 * 删除路径下的所有文件
	 * 
	 * @param directory
	 *            路径
	 */
	public void deleteFilesByDirectory(File directory) {
		if (directory != null && directory.exists() && directory.isDirectory()) {
			for (File item : directory.listFiles()) {
				item.delete();
			}
		}
	}

	/**
	 * 清除项目缓存文件
	 */
	public void cleanCache() {
		deleteFilesByDirectory(context.getCacheDir());
	}

	public void delete(File file) {
		if (file.isFile()) {
			file.delete();
			return;
		}
		if (file.isDirectory()) {
			File[] childFiles = file.listFiles();
			if (childFiles == null || childFiles.length == 0) {
				file.delete();
				return;
			}

			for (int i = 0; i < childFiles.length; i++) {
				delete(childFiles[i]);
			}
			file.delete();
		}
	}
}
